package com.example.question.tree.tree3;

import com.example.question.tree.tree1.TreeNode;

public class Code02_617 {
    public static void main(String[] args) {

    }


    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }
        if (root1 == null) {
            return root2;
        }

        if (root2 == null) {
            return root1;
        }
        return merge(root1, root2);
    }

    private TreeNode merge(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return null;
        }
        int val = 0;
        if (node1 != null) {
            val += node1.val;
        }
        if (node2 != null) {
            val += node2.val;

        }
        TreeNode node = new TreeNode(val);
        node.left = merge(node1 == null ? null : node1.left, node2 == null ? null : node2.left);
        node.right = merge(node1 == null ? null : node1.right, node2 == null ? null : node2.right);
        return node;
    }
}
